package com.stone.stoneanswer.controller;

import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.nodes.Http;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stone.stoneanswer.common.BaseResponse;
import com.stone.stoneanswer.common.DeleteRequest;
import com.stone.stoneanswer.common.ErrorCode;
import com.stone.stoneanswer.common.ResultUtils;
import com.stone.stoneanswer.exception.BusinessException;
import com.stone.stoneanswer.exception.ThrowUtils;
import com.stone.stoneanswer.model.dto.answer.AnswerResultAddRequest;
import com.stone.stoneanswer.model.dto.answer.AnswerResultGetRequest;
import com.stone.stoneanswer.model.dto.post.PostAddRequest;
import com.stone.stoneanswer.model.entity.AnswerResult;
import com.stone.stoneanswer.model.entity.Post;
import com.stone.stoneanswer.model.entity.User;
import com.stone.stoneanswer.model.vo.AnswerResultVO;
import com.stone.stoneanswer.service.AnswerResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/answer")
@Slf4j
public class AnswerResultController {

    @Resource
    private AnswerResultService answerResultService;

    @PostMapping("/add")
    public BaseResponse<Long> answerQuestion(@RequestBody AnswerResultAddRequest answerResultAddRequest , HttpServletRequest request) {
        // 进行参数校验
        if (answerResultAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long result = answerResultService.answerQuestion(answerResultAddRequest , request);
        return ResultUtils.success(result);
    }

    @GetMapping("/get/vo/by/bankId")
    public BaseResponse<AnswerResultVO> getAnswerResultVOByBankId(Long bankId , HttpServletRequest request){
        if(bankId<0){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR , "bankId不能小于0");
        }
        return ResultUtils.success(answerResultService.getAnswerResultByBankId(bankId , request));
    }

    @GetMapping("/get/vo/by/id")
    public BaseResponse<AnswerResultVO> getAnswerResultVOById(Long id){
        if(id<0){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR , "bankId不能小于0");
        }
        return ResultUtils.success(AnswerResultVO.objToVo(answerResultService.getById(id)));
    }

    @PostMapping("/list/vo/my")
    public BaseResponse<Page<AnswerResultVO>> listMyAnswerResultVOByPage(@RequestBody AnswerResultGetRequest answerResultGetRequest , HttpServletRequest request){
        return ResultUtils.success(answerResultService.listMyAnswerResultVOByPage(answerResultGetRequest , request));
    }

    @PostMapping("/list/vo")
    public BaseResponse<Page<AnswerResultVO>> listAnswerResultVOByPage(@RequestBody AnswerResultGetRequest answerResultGetRequest , HttpServletRequest request){
        return ResultUtils.success(answerResultService.listAnswerResultVOByPage(answerResultGetRequest , request));
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteAnswerResult(@RequestBody DeleteRequest request){
        if(request.getIds()!=null&&!request.getIds().isEmpty()){
            return ResultUtils.success(answerResultService.removeBatchByIds(request.getIds()));
        }else if(request.getId()!=null&&request.getId()>0){
            return ResultUtils.success(answerResultService.removeById(request.getId()));
        }
        else{
            throw new BusinessException(ErrorCode.PARAMS_ERROR , "删除答题结果时传入的ID参数为空");
        }
    }

    @GetMapping("/analyze/{answerResultId}")
    public SseEmitter getAnalyzeResult(@PathVariable("answerResultId")Long answerResultId){
        if(answerResultId <=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR , "answerResultId<=0，不合法");
        }
        return answerResultService.getAnalyzeResult(answerResultId);
    }
}
